package leetcode;

import java.util.Arrays;

public class lc128 {
    public static void main(String[] args) {
        longestConsecutive(new int[]{1,2,0,1});
    }
    /**
     * Input: nums = [100,4,200,1,3,2]
     * Output: 4
     * Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
     */
    public static int longestConsecutive(int[] nums) {
        if(nums.length == 0) return 0;
        Arrays.sort(nums);
        int[] dupRmArr = Arrays.stream(nums).distinct().toArray();
        int n = dupRmArr.length;
        int i = 0;
        int ret = 1;
        for(int j=i+1;j < n;++j){
            while(j<n && (dupRmArr[j]-1 == dupRmArr[j-1])) ++j;
            ret = Math.max(ret, j - i);
            i=j;
        }
        return ret;
    }
}
